package com.jingHang.controller;

import com.jingHang.common.Const;
import com.jingHang.common.ResponseCode;
import com.jingHang.common.ServerResponse;
import com.jingHang.pojo.User;
import com.jingHang.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller//加上控制层的注解
//定义url映射路径
@RequestMapping(value = "/user")
public class UserController {

    //自动注入对象，spring ioc容器的功能
    @Autowired
    IUserService iUserService;

   /* //定义url映射路径
    @RequestMapping("login.do")
    //序列化对象
    @ResponseBody
    public ServerResponse login(String userName, String password) {
        return iUserService.login(userName, password);
    }*/


    @RequestMapping(value = "login.do")
    @ResponseBody
    public ServerResponse<User> login(String userName,
                                      String password,
                                      HttpServletRequest request,
                                      HttpSession session) {
        ServerResponse<User> response = iUserService.login(userName, password);
        if (response.isSuccess()) {
            //设置值，类似map数据类型
            //设置session的值  key:currentUser  value:User
            User user = response.getData();
            session.setAttribute(Const.CURRENT_USER, user);
            session.setAttribute("time",System.currentTimeMillis());
            session.setAttribute("1","1");
        }
        return response;
    }

    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "logout.do")
    @ResponseBody
    public ServerResponse<String> logout(HttpSession session) {
        String key = Const.CURRENT_USER;
        session.removeAttribute(key);
        return ServerResponse.createBySuccess("退登录成功");
    }

    /**
     * 需求：
     * 1.用户名不能重复
     * 2.邮箱不能重复注册
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "register.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<String> register(User user) {
        return iUserService.register(user);
    }


    /**
     * 更新用户（user）信息
     * @param user
     * @return
     */
    /*@RequestMapping(value = "update_information.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<User> update_information(User user){
        return iUserService.updateInformation(user);
    }*/


    /**
     * 更新用户（user）信息
     *
     * @param session
     * @param user
     * @return
     */
    @RequestMapping(value = "update_information.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<User> update_information(HttpSession session, User user) {
        //根据key获取value  (key=currentUser)
        String key = Const.CURRENT_USER;
        User currentUser = (User) session.getAttribute(key);

        if (currentUser == null) {
            return ServerResponse.createByError("用户未登录");
        }

        user.setId(currentUser.getId());

        ServerResponse<User> response = iUserService.updateInformation(user);

        if (response.isSuccess()) {
            response.getData().setUsername(currentUser.getUsername());
            session.setAttribute(Const.CURRENT_USER, response.getData());
        }

        return response;
    }


    /**
     * 获取用户信息
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "get_information.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<User> get_information(HttpServletRequest request,HttpSession session) {
        String key = Const.CURRENT_USER;
        User currentUser = (User) session.getAttribute(key);

        if (currentUser == null) {
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(), "未登录,需要强制登录status=10");
        }

        return iUserService.getInformation(currentUser.getId());
    }
}
